package nl.utwente.eemcs.graph.distribution;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import nl.utwente.eemcs.graph.Edge;
import nl.utwente.eemcs.graph.Node;


public class DistributionList {
	private HashMap<Node, DistributionLocation> nodeDistributions;
	private HashMap<Edge, DistributionLocation> edgeDistributions;
	private HashMap<String, DistributionLocation> dataItemRestrictions;
	
	public DistributionList() {
		this.nodeDistributions = new HashMap<Node, DistributionLocation>();
		this.edgeDistributions = new HashMap<Edge, DistributionLocation>();
		this.dataItemRestrictions = new HashMap<String, DistributionLocation>();
	}
	
	public HashMap<String, DistributionLocation> getDataItemRestrictions() {
		return dataItemRestrictions;
	}

	public void setDataItemRestrictions(HashMap<String, DistributionLocation> dataItemRestrictions) {
		this.dataItemRestrictions = dataItemRestrictions;
	}

	public HashMap<Node, DistributionLocation> getNodeDistributions() {
		return nodeDistributions;
	}
	
	public HashMap<Edge, DistributionLocation> getEdgeDistributions() {
		return edgeDistributions;
	}
	
	public void addNodeDistribution(Node n, DistributionLocation d) {
		nodeDistributions.put(n, d);
	}
	
	public void addEdgeDistribution(Edge e, DistributionLocation d) {
		edgeDistributions.put(e, d);
	}	
	
	public void addDataRestriction(String s, DistributionLocation d) {
		dataItemRestrictions.put(s, d);
	}		
	
	public DistributionLocation getDistributionLocation(Node n) {
		if (nodeDistributions.containsKey(n))
			return nodeDistributions.get(n);
		else {
			return DistributionLocation.OnPremise;
		}
	}
	
	public DistributionLocation getRestriction(String item) {
		if (hasDataRestriction(item))
			return this.dataItemRestrictions.get(item);
		else
			return null;
	}	
	
	public boolean hasDistributionLocation(Node node) {
		return nodeDistributions.containsKey(node);
	}	
	
	public boolean hasDistributionLocation(Edge edge) {
		return edgeDistributions.containsKey(edge);
	}		
	
	public boolean hasDataRestriction(String item) {
		return this.dataItemRestrictions.containsKey(item);
	}		

	public DistributionLocation getDistributionLocation(Edge e) {
		if (edgeDistributions.containsKey(e))
			return edgeDistributions.get(e);
		else
			return DistributionLocation.OnPremise;
	}
	
	public List<Node> getNodes(DistributionLocation... locations) {
		List<Node> result = new ArrayList<Node>();
		List<DistributionLocation> locationsList = Arrays.asList(locations);
		
		for (Node n : this.nodeDistributions.keySet()) {
			if (locationsList.size() == 0 || locationsList.contains(this.nodeDistributions.get(n)))
				result.add(n);
		}
		
		return result;
	}
}
